package com.inossem.warehousestorage_inossem.adapter.changeStorage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.change_storage.ChangeStorageSelectMaterielActivity;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageSelectMaterielBean;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class ChangeStorageSelectMaterielAdapter extends BaseAdapter {

    private Context mContext;
    private List<ChangeStorageSelectMaterielBean.MaterielListBean> mMaterielList;
    private LayoutInflater inflater;
    private ChangeStorageSelectMaterielActivity.ChangeStorageOtherSelectSpecialInventoryListener mInStorageOtherSelectSupplierListener;
    private String mMaterielCode;

    public ChangeStorageSelectMaterielAdapter(Context context, List<ChangeStorageSelectMaterielBean.MaterielListBean> materielList, String materielCode, ChangeStorageSelectMaterielActivity.ChangeStorageOtherSelectSpecialInventoryListener inStorageOtherSelectSupplierListener) {
        mContext = context;
        mMaterielList = materielList;
        mMaterielCode = materielCode;
        mInStorageOtherSelectSupplierListener = inStorageOtherSelectSupplierListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mMaterielList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mMaterielList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_other_select_supplier_list_first_item, null);
            EditText materielCode = (EditText) convertView.findViewById(R.id.supplierCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            materielCode.setText(mMaterielCode);
            materielCode.setHint(R.string.activity_change_storage_select_please_input_material_code);
            search.setOnClickListener(mInStorageOtherSelectSupplierListener.getNoDoubleClickListener(materielCode));
        } else {
            ChangeStorageSelectMaterielBean.MaterielListBean materielItem = mMaterielList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_in_storage_other_select_supplier_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, materielItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.code = (TextView) view.findViewById(R.id.code);
        viewHolder.description = (TextView) view.findViewById(R.id.description);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, ChangeStorageSelectMaterielBean.MaterielListBean materielItem) {
        viewHolder.code.setText(materielItem.getMat_code());
        viewHolder.description.setText(materielItem.getMat_name());
        view.setOnClickListener(mInStorageOtherSelectSupplierListener.select(materielItem.getMat_code(), materielItem.getMat_name() , materielItem.getMat_id()));
    }

    private class ViewHolder {
        private TextView code, description;
    }

}
